package com.tourism.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tourism.core.common.enums.CommentStatusEnum;
import com.tourism.domain.dto.comment.CommentApproveDTO;
import com.tourism.domain.dto.comment.CommentQueryDTO;
import com.tourism.domain.entity.Comment;
import com.tourism.mapper.CommentMapper;
import com.tourism.service.CommentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 评论服务实现类
 *
 * @author lucky
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
        implements CommentService {

    @Override
    public Page<Comment> getPage(Integer pageIndex, Integer pageSize, CommentQueryDTO commentQueryDTO) {
        Page<Comment> page = new Page<>(pageIndex, pageSize);
        List<Comment> records = baseMapper.getAllComments(page, commentQueryDTO);
        page.setRecords(records);
        return page;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean approve(CommentApproveDTO commentApproveDTO) {
        List<Long> ids = commentApproveDTO.getIds();
        String status = commentApproveDTO.getStatus();

        if(ids.isEmpty()){
            return true;
        }

        // 根据id查询待审核的评论
        LambdaQueryWrapper<Comment> lqw = new LambdaQueryWrapper<>();
        lqw.in(Comment::getId, ids);
        lqw.eq(Comment::getStatus, CommentStatusEnum.PENDING.getCode());
        List<Comment> comments = list(lqw);

        // 构建更新列表
        List<Comment> updateList = comments.stream()
                .map(comment -> {
                    Comment c = new Comment();
                    c.setId(comment.getId());
                    c.setStatus(status);

                    return c;
                }).toList();


        // 批量更新
        updateBatchById(updateList);

        return true;
    }
}




